package com.df.bpm.utils.businessViewerV2.informatization;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.business.C_business;
import com.df.bpm.utils.businessViewerV2.entity.C_entity;
import com.df.bpm.utils.businessViewerV2.entity.M_entityDto;
import com.df.bpm.utils.businessViewerV2.res.V_scrollPanel;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class V_scroll4Output extends V_scrollPanel<M_outputDTO> {

    M_requirement requirementBasic;

    @Override
    public List<String> getColNames() {
        return Arrays.asList("ID", "序号","实体ID","输出物名称","创建时间","更新时间");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
        Main.hideCol(iTable,2);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,3,1,11,11,11));
    }

    @Override
    public Object[] getRowValues(M_outputDTO row) {
        return new Object[]{row.getInformatizationOutputIndex(),-1,
                row.getBusinessEntityIndex(),row.getEntityName(),
                dateFormat.format(row.getCreateDate()),
                dateFormat.format(row.getUpdateDate())};
    }

    @Override
    public M_outputDTO getEmptyRow() {
        return new M_outputDTO();
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_outputDTO> list) {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0) {
                    if (e.getClickCount() == 2) {// 处理双击事件
                        if ((col == 3)) {//编辑需求改善目标
                            Object oldValue = iTable.getValueAt(row, 2);
                            M_entityDto temp=C_entity.selectOne();
                            if (temp != null && !temp.getBusinessEntityIndex().equals(oldValue)) {
                                list.get(row).setBusinessEntityIndex(temp.getBusinessEntityIndex());
                                list.get(row).setEntityName(temp.getName());
                                iTable.setValueAt(temp.getName(), row, col);
                                if (row == list.size() - 1) {
                                    list.add(getEmptyRow());
                                    ((DefaultTableModel)iTable.getModel()).addRow(new Object[getColNames().size()]);
                                }
                                if(list.get(row).getInformatizationOutputIndex()!=null){
                                    if(temp.getBusinessEntityIndex()==-1){//删
                                        C_requirement.deleteOutput(list.get(row));
                                        iTable.setValueAt(null, row, 0);
                                        list.get(row).setInformatizationOutputIndex(null);
                                    }else{//改
                                        if(requirementBasic!=null &&requirementBasic.getInformatizationRequirementIndex()!=null){
                                            list.get(row).setInformatizationRequirementIndex(requirementBasic.getInformatizationRequirementIndex());
                                        }
                                        C_requirement.updateOutput(list.get(row));
                                    }
                                }else{//增
                                    if(requirementBasic!=null && requirementBasic.getInformatizationRequirementIndex()!=null){
                                        list.get(row).setInformatizationRequirementIndex(requirementBasic.getInformatizationRequirementIndex());
                                        M_outputDTO mOutput = C_requirement.addOutput(requirementBasic, list.get(row));
                                        list.set(row,mOutput);
                                        iTable.setValueAt(dateFormat.format(mOutput.getCreateDate()), row, 4);
                                    }
                                }
                            }
                        } else if (col==1) {
                            C_business.showBusinessDtoByOutput(requirementBasic.getInformatizationRequirementIndex(),list.get(row));
                        }

                    }
                }
            }
        };
    }

    public void setRequirementBasic(M_requirement basic) {
        this.requirementBasic=basic;
    }
}
